using System;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;

namespace Glpt.Office
{
    /// <summary>
    /// 导出Word类
    /// </summary>
    public class ExportWord
    {
        private float TopMargin = 0f;
        private float BottomMargin = 0f;
        private float LeftMargin = 0f;
        private float RightMargin = 0f;
        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public ExportWord()
        {
        }
        /// <summary>
        /// 参数设置
        /// </summary>
        /// <param name="param_list">参数</param>
        public void SetParams(string[] param_list)
        {
            if (param_list.Length > 0)
                TopMargin = float.Parse(param_list[0]);
            if (param_list.Length > 1)
                BottomMargin = float.Parse(param_list[1]);
            if (param_list.Length > 2)
                LeftMargin = float.Parse(param_list[2]);
            if (param_list.Length > 2)
                RightMargin = float.Parse(param_list[3]);
        }
        /// <summary>
        /// 参数设置
        /// </summary>
        /// <param name="topMargin">topMargin</param>
        /// <param name="bottomMargin">bottomMargin</param>
        /// <param name="leftMargin">leftMargin</param>
        /// <param name="rightMargin">rightMargin</param>
        public void SetParams(float topMargin, float bottomMargin, float leftMargin, float rightMargin)
        {
            TopMargin = topMargin;
            BottomMargin = bottomMargin;
            LeftMargin = leftMargin;
            RightMargin = rightMargin;
        }
        /// <summary>
        /// HTML转存为Word
        /// </summary>
        /// <param name="filename">源文件</param>
        /// <param name="savefilename">目标文件</param>
        public void HtmlToWord(string filename, string savefilename)
        {
            Word.Application wordApp = new Word.Application();
            Word.Document wordDoc = null;
            Object Nothing = Missing.Value;
            try
            {
                Object path = filename as Object;
                Object path2 = savefilename as Object;
                Object format = Word.WdSaveFormat.wdFormatDocument;
                wordDoc = wordApp.Documents.Open(ref path, false); //打开之前生成的文件
                wordDoc.Activate();//设为当前操作的文件
                //指定要在页面视图中显示的文档元素
                wordApp.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekMainDocument;//设为主文档
                if (TopMargin > 0)
                    wordDoc.PageSetup.TopMargin = TopMargin;
                if (BottomMargin > 0)
                    wordDoc.PageSetup.BottomMargin = BottomMargin;
                if (LeftMargin > 0)
                    wordDoc.PageSetup.LeftMargin = LeftMargin;
                if (RightMargin > 0)
                    wordDoc.PageSetup.RightMargin = RightMargin;
                wordDoc.PageSetup.HeaderDistance = 0.0f;//页眉位置
                //设置文档为页面视图模式
                wordApp.ActiveWindow.View.Type = Word.WdViewType.wdNormalView;
                wordApp.ActiveWindow.ActivePane.Selection.WholeStory();
                //指定要应用于段落的行距格式
                //wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpace1pt5;//1.5 倍行距
                //设置指定段落的段后间距
                //wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.LineUnitAfter = 0.5f;
                //把操作后的文件保存到路径2
                wordDoc.SaveAs(ref path2, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            }
            finally
            {
                if (wordDoc != null)
                    wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                if (wordApp != null)
                    wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
            }
        }
        /// <summary>
        /// HTML转存为Word
        /// </summary>
        /// <param name="filename">源文件</param>
        /// <param name="savefilename">目标文件</param>
        public static void WordToPDF(string filename, string savefilename)
        {
            Word.Application wordApp = new Word.Application();
            Word.Document wordDoc = null;
            Object Nothing = Missing.Value;
            try
            {
                wordApp.Visible = false;
                wordDoc = wordApp.Documents.Open(filename);
                wordDoc.ExportAsFixedFormat(savefilename, Word.WdExportFormat.wdExportFormatPDF);
            }
            finally
            {
                if (wordDoc != null)
                    wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                if (wordApp != null)
                    wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
            }
        }
    }
}
